package com.asha.md360player4android.vr.projection;

import android.content.Context;
import android.graphics.RectF;
import android.opengl.Matrix;

import com.asha.md360player4android.vr.objects.MDCircleExtra;
import com.asha.md360player4android.vr.plugins.FYPanoramaPluginEx;
import com.asha.vrlib.MD360Director;
import com.asha.vrlib.model.MDMainPluginBuilder;
import com.asha.vrlib.objects.MDAbsObject3D;
import com.asha.vrlib.objects.MDObject3DHelper;
import com.asha.vrlib.plugins.MDAbsPlugin;
import com.asha.vrlib.strategy.projection.DomeProjection;

/**
 * Created by yao.fu on 17-4-13.
 */

public class FYDomeProjectionFBO extends DomeProjection {


    private MDCircleExtra mCircleObject;

    public FYDomeProjectionFBO(RectF textureSize, float degree, boolean isUpper) {
        super(textureSize, degree, isUpper);
    }

    @Override
    public void turnOnInGL(Context context) {
        super.turnOnInGL(context);

        mCircleObject = new MDCircleExtra();
        MDObject3DHelper.loadObj(context, mCircleObject);
    }

    @Override
    public MDAbsPlugin buildMainPlugin(MDMainPluginBuilder builder) {
        return new FYPanoramaPluginEx(builder);
    }

    @Override
    public MDAbsObject3D getObject3DPlus() {
        return mCircleObject;
    }

    @Override
    public MD360Director getMD360DirectorPlus() {
        return new OrthogonalDirector(new MD360Director.Builder());
    }

    private class OrthogonalDirector extends MD360Director {

        private OrthogonalDirector(Builder builder) {
            super(builder);
            setEyeZ(-200);
        }

        @Override
        public void setDeltaX(float mDeltaX) {
            // nop
        }

        @Override
        public void setDeltaY(float mDeltaY) {
            // nop
        }

        @Override
        public void updateSensorMatrix(float[] sensorMatrix) {
            // nop
        }

        @Override
        protected void updateProjection() {
            final float left = -0.5f;
            final float right = 0.5f;
            final float bottom = -0.5f;
            final float top = 0.5f;
            final float far = 500;
            Matrix.orthoM(getProjectionMatrix(), 0, left, right, bottom, top, getNear(), far);
        }
    }
}
